<?php


namespace app\admin\controller;


use app\admin\model\Dictionary;
use Dotenv\Dotenv;
use JiraRestApi\Configuration\ArrayConfiguration;
use JiraRestApi\Configuration\DotEnvConfiguration;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\TimeTracking;
use JiraRestApi\JiraException;
use JiraRestApi\Project\ProjectService;

class Test extends Permissions
{

    public function test(){

        echo
        var_dump(ROOT_PATH);die;
        $iss = new IssueService(new ArrayConfiguration(
            array(
                'jiraHost' => 'https://jira.corp.dycd.com/',
                // for basic authorization:
                'jiraUser' => 'xinpeng',
                'jiraPassword' => 'xp881203',
                // to enable session cookie authorization (with basic authorization only)
                'cookieAuthEnabled' => true,
                //'cookieFile' => storage_path('jira-cookie.txt'),
            )
        ));
        var_dump($iss);
    }
    public function test1(){
        try {
            $proj = new ProjectService();


            $p = $proj->get('10300');

            var_dump($p);
        } catch (JiraException $e) {
            print("Error Occured! " . $e->getMessage());
        }
    }

    public function projectList(){
        try {
            $proj = new ProjectService();

            $prjs = $proj->getAllProjects();

//            var_dump($prjs);
            foreach ($prjs as $p) {
                echo sprintf("Project Key:%s, Id:%s, Name:%s, projectCategory:%s <br/>",
                    $p->key, $p->id, $p->name, !empty($p->projectCategory)?$p->projectCategory['name']:'暂无'
                );
                //var_dump($p);
            }
        } catch (JiraException $e) {
            print("Error Occured! " . $e->getMessage());
        }
    }

    /**
     * 获取项目信息
     * @throws \JsonMapper_Exception
     */
    public function getProjectInfo(){
        $projectId = '10300';
        try {
            $proj = new ProjectService();


            $p = $proj->get($projectId);

            var_dump($p);
        } catch (JiraException $e) {
            print("Error Occured! " . $e->getMessage());
        }
    }

    public function getIssueInfo(){
        try {
            $issueService = new IssueService();
            $queryParam = [
                'fields' => [  // default: '*all'
//                    'summary',
//                    'comment',
                ],
                'expand' => [
                    'renderedFields',
                    'names',
                    'schema',
                    'transitions',
                    'operations',
                    'editmeta',
                    'changelog',
                ]
            ];

            $issue = $issueService->get('AUTO-35', $queryParam);

            dump($issue->fields->summary);
            dump($issue->fields->getIssueType()->name);
            var_dump($issue->fields);
        } catch (JiraException $e) {
            print("Error Occured! " . $e->getMessage());
        }
    }

    public function createIssue(){
        try {
            $issueField = new IssueField();
            $timeTracking = new TimeTracking();

            $timeTracking->setOriginalEstimate('3w 4d 2h');
            $timeTracking->setRemainingEstimate('1w 2d 3h');

            $issueField->setProjectKey("AUTO")
                ->setSummary("something's wrong!!!!")
                ->setAssigneeName("xinpeng")
                ->setPriorityName("Highest")//Highest,Medium,Low
                ->setIssueType("通用任务")
                ->setDescription("描述描述描述描述描述描述描述描述描述描述描述描述")
                //->addVersion(["1.0.1", "1.0.3"])
                //->addComponents(['Component-1', 'Component-2'])
                // set issue security if you need.
                //->setSecurityId(10001 /* security scheme id */)
                ->setDueDate('2020-06-19')
//                ->addCustomField('timetracking_originalestimate','2d')
//                ->addCustomField('originalEstimate','3d')
//                ->addCustomField('remainingEstimate','4d')
                ->setTimetracking($timeTracking)
//                ->setOriginalEstimate('3d')
//                ->setRemainingEstimate('4d')
//                ->setOriginalestimate1('3d')
//                ->setRemainingestimate1('4d')
            ;
//
//            $issueField->setProjectKey("AUTO")
//                ->setSummary("something's wrong")
////                ->setAssigneeName("xinpeng")
//                ->setPriorityName("Highest")
//                ->setIssueType("Bug")
////                ->setDescription("Full description for issue")
////                ->addVersion(["1.0.1", "1.0.3"])
////                ->addComponents(['Component-1', 'Component-2'])
//                // set issue security if you need.
////                ->setSecurityId(10001 /* security scheme id */)
////                ->setDueDate('2019-06-19')
//            ;


            $issueService = new IssueService();
            //$issueService->timeTracking("AUTO", $timeTracking);

            $ret = $issueService->create($issueField);

            //If success, Returns a link to the created issue.
            var_dump($ret);
        } catch (JiraException $e) {
            print("Error Occured! " . $e->getMessage());
        }
    }

    public function updateIssue(){
        $issueKey = "AUTO-35";

        try {
            $issueField = new IssueField(true);

            $issueField
//                ->setSummary("something's wrong")
                ->setAssigneeName("xinpeng")//经办人
//                ->setIssueType("Bug")
                ->setPriorityName("Medium")//Highest,Medium,Low
//                ->setDescription("This is a shorthand for a set operation on the summary field")
            ;

            // optionally set some query params
            $editParams = [
                'notifyUsers' => false,
            ];
echo '11';
            $issueService = new IssueService();
echo '22';
            // You can set the $paramArray param to disable notifications in example
            $ret = $issueService->update($issueKey, $issueField, $editParams);
            echo '33';
            var_dump($ret);
        } catch (JiraException $e) {
            print("Error Occured! " . $e->getMessage());
        }
    }


//    /**
//     * @param TimeTracking $timeTracking
//     * @return $this
//     */
//    public function setTimeTracking($timeTracking){
//        if ($timeTracking instanceof TimeTracking){
//            if (is_null($this->timetracking)){
//                $this->timetracking = new TimeTracking();
//            }
//            $this->timetracking = $timeTracking;
//        }
//        return $this;
//    }
//
//    public function getTimeTracking(){
//        return $this->timetracking;
//    }


public function testzjl(){
        dump(Dictionary::jiraProjectStagMap('20','jira'));
}

public function testarr($k = 50){
    $map = [
        'dev'=>[10,20,30],
        'test'=>[35,40,50,60],
        'other'=>[70],
    ];
$r = $map;
    array_walk($map,function ($value,$key) use(&$r,$k){
        if(in_array($k,$value)) {
            $r = $key;
            return $key;
        }
    });

    return $r;

}
}